import { createModuleLogger } from '@/utils/logger';
import { ChainMonitorConfig } from '../config/ChainMonitorConfigService';

const logger = createModuleLogger('base-chain-monitor');

export abstract class BaseChainMonitor {
  protected isRunning: boolean = false;
  protected chainConfig: ChainMonitorConfig | null = null;
  protected transactionCallback: ((transaction: any) => void) | null = null;

  constructor(protected chainName: string) {}

  // 设置链配置
  public async setChainConfig(config: ChainMonitorConfig): Promise<void> {
    this.chainConfig = config;
    logger.debug(`📋 ${this.chainName} 配置已设置:`, {
      rpcUrl: config.rpcUrl,
      minValueUsd: config.minValueUsd,
      isEnabled: config.isEnabled
    });
  }

  // 设置交易回调
  public setTransactionCallback(callback: (transaction: any) => void): void {
    this.transactionCallback = callback;
    logger.debug(`📋 ${this.chainName} 交易回调已设置`);
  }

  // 获取运行状态
  public get isMonitorRunning(): boolean {
    return this.isRunning;
  }

  // 抽象方法：子类必须实现
  public abstract start(): Promise<void>;
  public abstract stop(): Promise<void>;

  // 通用的交易处理方法
  protected handleTransaction(transaction: any): void {
    if (this.transactionCallback) {
      try {
        this.transactionCallback(transaction);
      } catch (error) {
        logger.error(`${this.chainName} 交易回调执行失败:`, error);
      }
    }
  }

  // 获取最小交易价值配置
  protected getMinTransactionValue(): number {
    return this.chainConfig?.minValueUsd || 5000;
  }

  // 获取RPC URL
  protected getRpcUrl(): string {
    if (!this.chainConfig?.rpcUrl) {
      throw new Error(`${this.chainName} RPC URL not configured`);
    }
    return this.chainConfig.rpcUrl;
  }

  // 获取WebSocket URL
  protected getWssUrl(): string | undefined {
    return this.chainConfig?.wssUrl;
  }
} 